package com.insmess.speech.sdk.util;

import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.Base64;
import java.util.UUID;

public class FileBase64Utils {
    public static String convertFileToBase64(String filePath) {
        File file = new File(filePath);
        byte[] fileContent = new byte[(int) file.length()];

        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            // 读取文件内容
            fileInputStream.read(fileContent);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 编码文件内容为Base64字符串
        return Base64.getEncoder().encodeToString(fileContent);
    }

    @SneakyThrows
    public static File convertBase64ToFile(String base64, String path) {
        byte[] decode = Base64.getDecoder().decode(base64);
        File file = new File(path);
        FileUtils.writeByteArrayToFile(file, decode);
        return file;
    }
}
